package _1连接数据库;

import java.sql.*;

public class Demo {
    public static void main(String[] args) throws SQLException {
        // 3.1 更新语句
        // 创建数据表语句
//		String sql = "CREATE TABLE student2 (id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(10),age INT);";
        // 增删改语句
//		String sql = "insert into student values (2,'李四',19)";
//		String sql = "delete from student where id = 1";
//		String sql = "update student set age=28 where id=2";





        //2.获取连接
        String url="jdbc:mysql://127.0.0.1/school";
        String username="root";
        String password="root";
        Connection conn = DriverManager.getConnection(url,username,password);

        //3定义SQL语句

//        String sql1 = "CREATE TABLE student (id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(10),age INT);";
//        String sql2 = "insert into student values (1,'zhangsan',19)";
//        String sql3 = "insert into student values (2,'李四',19)";
//        String sql4 = "insert into student values (3,'wangwu',19)";
//        String sql5 = "delete from student where id = 1";
//        String sql6 = "update student set age=28 where id=2";
//        //4.获取执行SQL对象的Statement
        Statement stmt=conn.createStatement();
//        //5.执行SQL
//        int result1=stmt.executeUpdate(sql1);
//        int result2=stmt.executeUpdate(sql2);
//        int result3=stmt.executeUpdate(sql3);
//        stmt.executeUpdate(sql4);
//        stmt.executeUpdate(sql5);
//        stmt.executeUpdate(sql6);
        //7.释放资源

        String sql7="select * from student";
        ResultSet rs=stmt.executeQuery(sql7);
        while(rs.next())
        {
            // 根据列得到得到每列内容
            int id = rs.getInt(1); //
            String name = rs.getString(2);
            int age = rs.getInt(3);
            System.out.println(id+"\t"+name+"\t"+age);
        }
        stmt.close();
        conn.close();
    }
}
